package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;

import com.itheima.mapper.MenuMapper;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findAll() {
        List<Menu> list = menuMapper.findAll();
        return list;
    }

    @Override
    public List<Integer> findSubMenuIdsByParentMenuId(Integer id) {
        List<Integer> list = menuMapper.findSubMenuIdsByParentMenuId(id);
        return list;
    }

    @Override
    public void edit(Integer[] menuIds, Menu menu, Integer[] roleIds) {
        if (menu.getLevel().equals("1")) {
            //看父菜单优先级是否重复
            Integer count = menuMapper.findCountByPriorityAndName(menu.getPriority(), menu.getName());
            if (count > 0) {
                throw new RuntimeException(MessageConstant.MENU_IS_REPEATED);
            } else {
                //设置路径
                Integer pathInteger = menu.getPriority() + 1;
                String path = pathInteger.toString();
                menu.setPath(path);
                //删除父菜单id
                menu.setParentMenuId(null);
                //更新父菜单
                menuMapper.updateParentMenu(menu);
                //设置子菜单的parentMenuId
                for (Integer menuId : menuIds) {
                    menuMapper.updateSubMenuParentIdById(menuId, menu.getId());
                }
                //删中间表的数据
                menuMapper.deleteRoleAndMenu(menu.getId());
                //设置角色表和菜单表的映射关系
                setMenuAndRole(roleIds, menu.getId());
            }
        } else {
            //看父菜单是否有子菜单
            Integer count = menuMapper.findCountByParentId(menu.getId());
            if (count > 0) {
                throw new RuntimeException(MessageConstant.Menu_IS_QUOTED);
            } else {
                String originalLevel = menuMapper.findLevelById(menu.getId());
                if (originalLevel.equals("1")) {
                    menu.setPath(null);
                }
                menuMapper.updateSubMenu(menu);
                //删中间表的数据
                menuMapper.deleteRoleAndMenu(menu.getId());
                //设置角色表和菜单表的映射关系
                setMenuAndRole(roleIds, menu.getId());
            }
        }
    }

    @Override
    public void deleteById(Integer id) {
        Integer countSubMenuById = menuMapper.findCountSubMenuById(id);
        if (countSubMenuById > 0) {
            throw new RuntimeException(MessageConstant.Menu_IS_QUOTED);
        }
        //先删中间表的数据
        menuMapper.deleteRoleAndMenu(id);
        //删主表
        menuMapper.deleteMenu(id);
    }

    @Override
    public void add(Integer[] menuIds, Menu menu, Integer[] roleIds) {
        //判断是否为父菜单
        if (menu.getLevel().equals("1")) {
            //看父菜单优先级是否重复
            Integer count = menuMapper.findCountByPriority(menu.getPriority());
            if (count > 0) {
                throw new RuntimeException(MessageConstant.MENU_IS_REPEATED);
            } else {
                //设置路径
                Integer pathInteger = menu.getPriority() + 1;
                String path = pathInteger.toString();
                menu.setPath(path);
                //添加父菜单
                menuMapper.add(menu);
                //设置子菜单的parentMenuId
                for (Integer menuId : menuIds) {
                    menuMapper.updateSubMenuParentIdById(menuId, menu.getId());

                }
                //将子菜单按照优先级排序查询出来，并设置路径
                List<Menu> list = menuMapper.findByParentId(menu.getId());
                for (int i = 0; i < list.size(); i++) {
                    String subPath = "/" + path + "-" + (i + 1);
                    menuMapper.updateSubMenuPathById(list.get(i).getId(), subPath);
                }
                //设置角色表和菜单表的映射关系
                setMenuAndRole(roleIds, menu.getId());
            }
        } else {
            //添加子菜单
            menuMapper.add(menu);
            setMenuAndRole(roleIds, menu.getId());
        }

    }

    private void setMenuAndRole(Integer[] roleIds, Integer id) {

        if (roleIds != null && roleIds.length > 0) {
            //将添加检查组和检查项的中间表数据
            menuMapper.setMenuAndRole(roleIds, id);

        }

    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        List<Menu> list = menuMapper.findByCondition(queryString);
        PageInfo<Menu> pageInfo = new PageInfo<>(list);

        return new PageResult(pageInfo.getTotal(), pageInfo.getList());

    }


        /**
         * 根据用户名获取自己私有的页面
         *
         * @param username 指定用户名
         * @return List<Menu>
         */
        @Override
        public List<Menu> getPersonallyPages(String username) {
            return menuMapper.getPersonallyPages(username);

        }
    }
